package com.qf.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qf.constant.MessageConstant;
import com.qf.entity.Result;
import com.qf.pojo.OrderSetting;
import com.qf.service.OrderSettingService;
import com.qf.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhouhao
 * @create 2021-12-23 16:43
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    public OrderSettingService orderSettingService;

    //上传excel文件
    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        //利用工具类上传
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);//使用POI解析表格数据
            //将解析的数据(数组类型)封装成对象
            ArrayList<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list){
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(orderDate));
                orderSetting.setNumber(Integer.parseInt(number));
                data.add(orderSetting);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            orderSettingService.add(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //根据月份查询对应的预约设置数据
    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        //为什么封装成List<Map>,根据我们之前测试的静态数据分析出来的
        try {
            List<Map<String,Object>> list =  orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,"查找成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查找失败");
        }
    }

    //设置预约人数
    @RequestMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.setNumber(orderSetting);
            return new Result(true,"设置预约人数成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"设置预约人数失败");
        }
    }
}
